package com.jiajia.study.chapter04;

import io.netty.channel.DefaultEventLoopGroup;
import io.netty.channel.EventLoop;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;

public class NettyFutureTest {
    private static final Logger log = LoggerFactory.getLogger(NettyFutureTest.class);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        DefaultEventLoopGroup defaultEventLoopGroup = new DefaultEventLoopGroup();

        EventLoop eventLoop = defaultEventLoopGroup.next();

        Future<Integer> future = eventLoop.submit(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                log.debug("执行了");
                Thread.sleep(10000);
                return 10;
            }
        });

        // 同步阻塞，在主线程拿到结果
//        Integer result = future.get();
//        log.debug("the result is {}", result);

        future.addListener(new GenericFutureListener<Future<? super Integer>>() {
            @Override
            public void operationComplete(Future<? super Integer> future) throws Exception {
                log.debug("the result is {}", future.get());
            }
        });

        log.debug("执行结束>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

    }
}
